/**
 * Created by John on 2015-04-12.
 */
var mysql = require('mysql');
var DB_NAME = 'wx_liao_user';

var pool  = mysql.createPool({
    host     : '127.0.0.1',
    database:'wx_liao_user',
    user     : 'root',
    password : 'u,4WmkdnAfDM4/H'
});
// var pool  = mysql.createPool({
//     host     : '127.0.0.1',
//     database:'wx_liao_user',
//     user     : 'root',
//     password : ''
// });
function Comment(comment){
    this.img_name = comment.img_name;
    this.comment_value = comment.comment_value;
};
module.exports = Comment;

pool.getConnection(function(err, connection) {
    var useDbSql = "USE " + DB_NAME;
    connection.query(useDbSql, function (err) {
        if (err) {
            console.log("USE Error: " + err.message);
            return;
        }
        console.log('USE succeed');
    });});


//保存数据
Comment.prototype.save = function save(callback) {
    console.log(this);
    console.log('我进来了');
    var comment = {
        'img_name': this.img_name,
        'comment_value': this.comment_value
    };
    pool.getConnection(function(err, connection) {

        var insertUser_Sql = "INSERT INTO comment(img_name,comment_value) VALUES(?,?)";

        connection.query(insertUser_Sql, [comment.img_name,comment.comment_value], function (err,result) {
            if (err) {
                console.log("insertUser_Sql Error: " + err.message);
                return;
            }
            connection.release();
            console.log("invoked[save]");
            callback(err,result);
        });
    });
};

Comment.prototype.getUserByUsernum = function getUserByUsernum(username, callback) {
        pool.getConnection(function(err, connection) {
    var getUserByUserName_Sql = "SELECT comment_value FROM comment WHERE img_name = ?";

    connection.query(getUserByUserName_Sql, [username], function (err, result) {
        if (err) {
            console.log("getUserByUserName Error: " + err.message);
            return;
        }

        connection.release();

        console.log("invoked[getUserByUserName]");
        callback(err,result);
        });
        });
};
Comment.prototype.getUserByUserallnum = function getUserByUserallnum(username, callback) {
        pool.getConnection(function(err, connection) {
    var getUserByUserName_Sql = "select img_name, sum(num) as 'zan_num' from comment group by img_name";

    connection.query(getUserByUserName_Sql, function (err, result) {
        if (err) {
            console.log("getUserByUserName Error: " + err.message);
            return;
        }

        connection.release();

        console.log("invoked[getUserByUserName]");
        callback(err,result);
        });
        });
};
Comment.prototype.getUser_admin_phone = function getUser_admin_phone(username, callback) {
        pool.getConnection(function(err, connection) {
    var getUserByUserName_Sql = "SELECT phone_number FROM "+username;

    connection.query(getUserByUserName_Sql, [], function (err, result) {
        if (err) {
            console.log("getUserByUserName Error: " + err.message);
            return;
        }

        connection.release();

        console.log("invoked[getUserByUserName]");
        callback(err,result);
        });
        });
};
